package com.example.springbootlearn.netty.handler;


import cn.hutool.json.JSONUtil;
import com.example.springbootlearn.netty.annotation.NettyHandler;
import com.example.springbootlearn.netty.dto.BizMsg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@ChannelHandler.Sharable
@NettyHandler(-1)
public class ObjectReceiveHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame textWebSocketFrame) {

        String text = textWebSocketFrame.text();
        BizMsg bean = JSONUtil.toBean(text, BizMsg.class);
//        log.info("收到消息：{}", bean);
        ctx.fireChannelRead(bean);

//        String text = textWebSocketFrame.text();
//        log.info("收到消息：{}", text);
//        ctx.channel().writeAndFlush(new TextWebSocketFrame(text));
    }
}